package com.example.blog.config;

import com.example.blog.util.ConstVariable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @description: 自定义用户登录拦截器
 * @author: chengbaiyi
 * @create: 2022-08-17 17:23
 */
@Component
public class LoginIntercept implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute(ConstVariable.USER_SESSION_KEY) != null) {
            return true;
        }
        // 跳转到登录页面
        response.sendRedirect("/login.html");
        return false;
    }
}
